﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO
{
    public partial class CompagnieManagement : System.Web.UI.Page
    {
        private CompagnieService compagnieService;

        protected void Page_Load(object sender, EventArgs e)
        {
            compagnieService = new CompagnieService(new CompagnieManager());

            #region binding gridView

            List<COMPAGNIE> compagnies = compagnieService.getAllCompagnies();
            if (compagnies == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (compagnies.Count == 0)
            {
                lblError.Text = "No company in the database";
                gdwCompagnies.DataBind();
            }

            else
            {
                lblError.Text = "";
                gdwCompagnies.DataSource = compagnies;
                gdwCompagnies.DataBind();
            }

            #endregion
        }

        protected void gdwCompagnies_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            compagnieService.createCompagnie(txtNom.Text);
            Response.Redirect("CompagnieManagement.aspx");
        }

        protected void btnModifier_Click(object sender, EventArgs e)
        {
            compagnieService.updateCompagnie(int.Parse(idUpdate.Value), txtNom.Text);

            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            Response.Redirect("CompagnieManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;
            txtNom.Text = "";
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            compagnieService.deleteCompagnie(int.Parse(((ImageButton)sender).CommandArgument));
            Response.Redirect("CompagnieManagement.aspx");
        }

        protected void update_Click(object sender, EventArgs e)
        {
            COMPAGNIE compagnie = compagnieService.getCompagnieById(int.Parse(((ImageButton)sender).CommandArgument));

            txtNom.Text = compagnie.NOM;
            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;

            idUpdate.Value = "" + compagnie.ID;

            hdfScrollIntoView.Value = "True";
        }

        protected void gdwCompagnies_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwCompagnies.DataSource = compagnieService.getAllCompagnies();
            gdwCompagnies.PageIndex = e.NewPageIndex;
            gdwCompagnies.DataBind(); 
        }
    }
}